package com.lysj.admin.utils.validation.validate;



import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.SpringContextUtil;
import com.lysj.admin.utils.spring.foundation.base.BaseEntity;
import com.lysj.admin.utils.spring.foundation.base.BaseService;
import com.lysj.admin.utils.validation.annotation.ForeignKey;
import com.lysj.admin.utils.web.ParamUtil;

/**
 * @author Created by zk on 2018-12-10 20:34
 * @description 外键校验算法
 */
public class ForeignKeyValidate extends AbstractValidate<ForeignKey,String>{
    @Override
    public void init(ForeignKey foreignKey) {

    }

    @Override
    public boolean isValid(String s) {
        if(ParamUtil.isBlank(s)){
            return true;
        }
        BaseService baseService = SpringContextUtil.getBean(annotation.service());
        BaseEntity baseEntity = baseService.findOne(s);
        if (baseEntity == null || baseEntity.getDelFlag().equals(CommonConstant.DEL_FLAG)) {
            return false;
        }
        return true;
    }
}
